package com.edu.chapter09_kotlin

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.edu.chapter09_kotlin.model.GameInfo
import com.edu.chapter09_kotlin.model.ItemBean

class MyAdapter : BaseQuickAdapter<GameInfo, QuickViewHolder>() {
    override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: GameInfo?) {
        if (item != null) {
            holder.setText(R.id.tv_item, item.introduction)
            Glide.with(holder.itemView.context)
                .load(item.icon)
                .placeholder(R.drawable.ic_launcher_background) // 加载中占位图
                .error(R.drawable.ic_launcher_foreground) // 加载失败显示
                .diskCacheStrategy(DiskCacheStrategy.ALL) // 缓存所有版本图片
                .into(holder.itemView.findViewById<View>(R.id.iv_item) as ImageView)
        }
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): QuickViewHolder {
        return QuickViewHolder(R.layout.item_list, parent)
    }
}